package com.factory.iamp.dao;

import com.factory.iamp.pojo.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository
public interface RoleMapper {
    @Select("select count(rid) from role")
    int Count();

    @Select("select * from role where name = #{name} limit 1")
    Role findByName(String name);

    @Select("select * from role limit #{offset}, #{limit}")
    List<Role> ListPaged(@Param("offset") int offset, @Param("limit") int limit);

    @Select("select * from role where rid = #{id} limit 1")
    Role findById(@Param("id") int rid);

    @Select("select `${name}` from role where rid = #{id} limit 1")
    Boolean findByIdName(@Param("id") int rid, @Param("name") String name);

    @Insert({
            "<script>",
            "insert into role (",
            "<foreach collection = 'data' item = 'item' index = 'index' separator = ','>",
            "${index}",
            "</foreach>)",
            " values (",
            "<foreach collection = 'data' item = 'item' index = 'index' separator = ','>",
            "#{item}",
            "</foreach>)",
            "</script>"
    })
    void addRole(@Param("data") HashMap<String, Object> data);

    @Delete("delete from role where rid = #{rid}")
    void deleteById(@Param("rid") int rid);
}
